package com.group3.group3.login.control;

import com.group3.group3.login.entity.MenuLevelEntity;
import com.group3.group3.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Locale;

@Controller
public class MainController {
    private Logger logger =  LogManager.getLogger(this.getClass().getName());
    @Resource
    private UserService userService;
    @RequestMapping("/")
    public String index(HttpServletRequest request){

        Locale locale = LocaleContextHolder.getLocale();
        List<MenuLevelEntity> queryMenuInfoByLang =userService.queryMenuInfoByLang(locale.toString());
        request.getSession().setAttribute("menuList",queryMenuInfoByLang);

//        select RM.MENU_ID from PT_ROLE_MENU RM ,PT_ROLE_USER RU
//        where RM.ROLE_UUID =  RU.ROLE_UUID and RU.USER_UUID = '001fddf8608c4cd1bbac81dde4e0c701'
        return "/login/main";
    }

    @RequestMapping("/getServiceMenu")
    @ResponseBody
    public List<MenuLevelEntity> queryMenuInfoByLang(@RequestParam(value = "menuId") String menuId,HttpServletRequest request){
        logger.info("menuId:" + menuId);
        List<MenuLevelEntity> list = (List<MenuLevelEntity>) request.getSession().getAttribute("menuList");
        for(MenuLevelEntity sysMenu:list){
            if(menuId.equals(sysMenu.getMenuId())){
                return sysMenu.getList();
            }
        }
        return  null;
    }

    @RequestMapping("/welcome")
    public String webcome(){
        return "login/webcome";
    }

}
